#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "tp2.h"

  void RemoveSK(char titulo[200], char buffer[200], PK *listaSK, int nSK, FILE *linvert, FILE *avail)
  {
    PK *found;
    char verifica[200],espaco=' ';
    int NRR,navail,NRRant,aux;
    found=BSearch(listaSK,buffer,0,nSK-1);
    NRR=found->NRR;
    fseek(linvert,NRR*204,0);
    fread(verifica,1,tmtitulo,linvert);
    if(VerificaIdent(titulo,verifica)==SAME){
      found->NRR=-1;
      fread(&navail,1,4,avail);
      fseek(linvert,(NRR*204),0);
      fwrite(&espaco,1,1,linvert);
      fwrite(&navail,1,4,linvert);
      fseek(avail,0,0);
      fwrite(&NRR,1,4,avail);
      return;
    }
    while(VerificaIdent(titulo,verifica)!=SAME){
      NRRant=NRR;
      fread(&NRR,1,4,linvert);
      fseek(linvert,NRR*204,0);
      fread(verifica,1,tmtitulo,linvert);
    }
    fseek(linvert,(NRR*204)+200,0);
    fread(&aux,1,4,linvert);
    fseek(linvert,(NRRant*204)+200,0);
    fwrite(&aux,1,4,linvert);
    fread(&navail,1,4,avail);
    fseek(linvert,(NRR*204),0);
    fwrite(&espaco,1,1,linvert);
    fwrite(&navail,1,4,linvert);
    fseek(avail,0,0);
    fwrite(&NRR,1,4,avail);
    return;
  }

  void Remove(PK *listaPK, PK *listaSKtitulo, PK *listaSKtipo, PK *listaSKautor, PK *listaSKano, PK *remove, int *nPK, int nSKtit, int nSKtip, int nSKaut, int nSKano)
  {
    FILE *file, *avail;
    char titulo[tmtitulo], tipo[tmtipo], autor[tmautor], ano[tmano], buffer[200], espaco=' ';
    int i=0, NRR, b;
    file=fopen("base20.dat","a+");
    avail=fopen("availbase.dat","r+");
    /*removendo da base*/
    fseek(file,(remove->NRR)*450,0);
    fread(titulo,1,tmtitulo,file);
    fread(tipo,1,tmtipo,file);
    fread(autor,1,tmautor,file);
    fread(ano,1,tmano,file);
    fseek(file,(remove->NRR)*450,0);
    fwrite(&espaco,1,1,file);
    fread(&NRR,1,4,avail);
    fwrite(&NRR,1,4,file);
    fwrite(&(remove->NRR),1,4,avail);
    /*removendo da PK*/
    *remove=listaPK[(*nPK)-1];
    (*nPK)--;
    if((*nPK)%20==0){
      listaPK=(PK*)realloc(listaPK,(*nPK)*sizeof(PK));
    }
    Ordena(listaPK,0,(*nPK));
    fclose(file);
    fclose(avail);
    file=fopen("listatitulo.dat","a+");
    avail=fopen("availtitulo.dat","r+");
    while((i<200)&&(titulo[i]!=' ')){
      while((i<200)&&(titulo[i]!=' ')){
        buffer[i]=titulo[i];
        i++;
      }
      b=i;
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      RemoveSK(titulo,buffer,listaSKtitulo,nSKtit,file,avail);
    }
    i=0;
    fclose(file);
    fclose(avail);
    file=fopen("listatipo.dat","a+");
    avail=fopen("availtipo.dat","r+");
    while((i<100)&&(tipo[i]!=' ')){
      while((i<100)&&(tipo[i]!=' ')){
        buffer[i]=tipo[i];
        i++;
      }
      b=i;
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      RemoveSK(titulo,buffer,listaSKtipo,nSKtip,file,avail);
    }
    i=0;
    fclose(file);
    fclose(avail);
    file=fopen("listaautor.dat","a+");
    avail=fopen("availautor.dat","r+");
    while((i<125)&&(autor[i]!=' ')){
      while((i<125)&&(autor[i]!=' ')){
        buffer[i]=autor[i];
        i++;
      }
      b=i;
      i++;
      while(b<200){
        buffer[b]=' ';
        b++;
      }
      RemoveSK(titulo,buffer,listaSKautor,nSKaut,file,avail);
    }
    i=0;
    fclose(file);
    fclose(avail);
    file=fopen("listaano.dat","a+");
    avail=fopen("availano.dat","r+");
    while(i<4){
      buffer[i]=ano[i];
      i++;
    }
    while(i<200){
      buffer[i]=' ';
      i++;
    }
    RemoveSK(titulo,buffer,listaSKano,nSKano,file,avail);
    fclose(file);
    fclose(avail);
    return;
  }
